package com.sevenGroup.pawnshop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sevenGroup.pawnshop.Utils.ParameterCalibrationUtil;
import com.sevenGroup.pawnshop.Utils.Result;
import com.sevenGroup.pawnshop.Utils.UploadImage;
import com.sevenGroup.pawnshop.controller.form.SelectStoreForm;
import com.sevenGroup.pawnshop.controller.form.StoreForm;
import com.sevenGroup.pawnshop.controller.form.pageForm;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.Stores;
import com.sevenGroup.pawnshop.service.StoresService;
import com.sevenGroup.pawnshop.service.WarehouseStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/stores")
@Api(tags = "处理与门店相关任务的接口")
public class StoresController {

    @Autowired
    private StoresService storesService;

    //查询所有门店
    @PostMapping("selectAllStore")
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @ApiOperation("查询所有门店")
    public Result selectAllStore(@ApiParam("需要传入分页对象")@Valid @RequestBody pageForm pageForm, BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        IPage iPage = storesService.selectAllStores(pageForm);
        return Result.success(iPage);
    }

    //查询所有显示的门店
    @PostMapping("selectAllStoreByStoreState")
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @ApiOperation("查询所有显示的门店")
    public Result selectAllStoreByStoreState(){
        List<Stores> list = storesService.list(new QueryWrapper<Stores>().eq("store_state", "正常").select("store_id","store_name"));
        return Result.success(list);
    }

    @ApiOperation("根据id查询门店信息")
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @GetMapping("selectStoreByStoreId/{storeId}")
    public Result selectStoreByStoreId(@PathVariable Integer storeId){
        Stores stores = storesService.getById(storeId);
        return Result.success(stores);
    }

    //新增门店
    @Transactional
    @PostMapping("saveStores")
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("新增门店")
    public Result saveStores(@ApiParam("需要传入门店对象")@Valid @RequestBody StoreForm storeForm,BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        storesService.saveStore(storeForm);
        return Result.success(null);
    }

    //根据id删除门店
    @PostMapping("delStore")
    @ApiOperation("根据id删除门店")
    @PreAuthorize("hasAnyAuthority('boss')")
    public Result delStore(@ApiParam("需要传入门店Id")Integer storeId) throws sevenGroupException {
        storesService.delStore(storeId);
        return Result.success(null);
    }

    //多选，根据集合来删除
    @PostMapping("delStoreByList")
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("多选，根据集合来删除")
    public Result delStoreByList(@ApiParam("需要传入门店id集合")@RequestBody ArrayList<Integer> list) throws sevenGroupException {
        storesService.delStoreByList(list);
        return Result.success(null);
    }

    //更新门店信息
    @PostMapping("updateStore")
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @ApiOperation("更新门店信息")
    public Result updateStore(@ApiParam("需要传入门店对象")@Valid @RequestBody StoreForm storeForm,BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        storesService.updateStore(storeForm);
        return Result.success(null);
    }

    //根据条件查询门店
    @PostMapping("selectAllStoreByAll")
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @ApiOperation("根据条件查询门店")
    public Result selectAllStoreByAll(@ApiParam("需要传入查询条件的对象")@RequestBody SelectStoreForm selectStoreForm) throws sevenGroupException {
        IPage iPage = storesService.selectAllStoreByAll(selectStoreForm);
        return Result.success(iPage);
    }

    @ApiOperation("根据门店id查询仓库")
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @GetMapping("selectStoreAndAllWarehouses/{storeId}")
    public Result selectStoreAndAllWarehouses(@PathVariable Integer storeId) throws sevenGroupException {
        Stores stores = storesService.selectStoreAndAllWarehouses(storeId);
        return Result.success(stores);
    }

    @ApiOperation("获取上传的图片地址")
    @PostMapping("GetURL")
    public Result GetURL(@RequestParam("uploadPicture")MultipartFile uploadPicture) throws IOException {
        String originalFilename = uploadPicture.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        long l = System.currentTimeMillis();
        String s = String.valueOf(l)+ substring;
        String URL = UploadImage.uploadImage(s, "2022-02-16", uploadPicture.getInputStream());
        System.out.println(URL);
        return Result.success(URL);
    }
}

